#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
第四阶段API简化测试脚本
"""

import requests
import json
import time

def test_api():
    """简化测试"""
    base_url = "http://127.0.0.1:5000"
    
    # 登录
    print("1. 测试登录...")
    login_response = requests.post(f"{base_url}/api/test-auth/login", json={
        "openid": "test_openid_001"
    })
    print(f"登录状态: {login_response.status_code}")
    if login_response.status_code == 200:
        data = login_response.json()
        token = data['data']['token']
        user_id = data['data']['user']['id']
        print(f"✅ 登录成功，用户ID: {user_id}")
        
        headers = {'Authorization': f'Bearer {token}', 'Content-Type': 'application/json'}
        
        # 测试择偶条件API
        print("\n2. 测试择偶条件更新...")
        criteria_data = {
            "min_age": 25,
            "max_age": 35,
            "education_preferences": ["本科", "硕士"],
            "income_preferences": ["10-20万"]
        }
        
        response = requests.put(f"{base_url}/api/users/v1/mate-criteria", 
                               json=criteria_data, headers=headers)
        print(f"择偶条件更新状态: {response.status_code}")
        if response.status_code == 200:
            print("✅ 择偶条件更新成功")
        else:
            print(f"❌ 更新失败: {response.text}")
        
        # 测试智能匹配
        print("\n3. 测试智能匹配...")
        response = requests.get(f"{base_url}/api/v1/matching/intelligent-match?page=1&per_page=3", 
                               headers=headers)
        print(f"智能匹配状态: {response.status_code}")
        if response.status_code == 200:
            result = response.json()
            print(f"✅ 智能匹配成功，找到 {len(result['data']['matches'])} 个匹配用户")
        else:
            print(f"❌ 智能匹配失败: {response.text}")
        
        # 测试匹配推荐
        print("\n4. 测试匹配推荐...")
        response = requests.post(f"{base_url}/api/v1/matching/recommendations", 
                                json={"recommendation_type": "daily", "count": 3}, 
                                headers=headers)
        print(f"匹配推荐状态: {response.status_code}")
        if response.status_code == 200:
            result = response.json()
            print(f"✅ 推荐成功，生成 {result['data']['total_count']} 个推荐")
        else:
            print(f"❌ 推荐失败: {response.text}")
        
        # 测试算法信息
        print("\n5. 测试算法信息...")
        response = requests.get(f"{base_url}/api/v1/matching/algorithms", headers=headers)
        print(f"算法信息状态: {response.status_code}")
        if response.status_code == 200:
            result = response.json()
            print(f"✅ 算法信息获取成功，有 {len(result['data']['algorithms'])} 个算法")
        else:
            print(f"❌ 获取失败: {response.text}")
        
        print("\n🎉 第四阶段API测试完成！")
    else:
        print(f"❌ 登录失败: {login_response.text}")

if __name__ == "__main__":
    test_api() 